package com.longrise.android.byjk.plugins.tabfirst;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.bumptech.glide.Glide;
import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.common.utils.PrintLog;
import com.longrise.common.utils.UmengStatisticsUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 首页banner的adapter
 * <p/>
 * <p/>
 * detailed comment
 *
 * @author Administrator 2015-12-30
 * @see
 * @since 1.0
 */
public class HomepagerAdapter extends PagerAdapter {

    private static final String TAG = "HomepagerAdapter";
    private Context mContext;
    private OnItemSelecter mSelecter;
    private List<EntityBean> mBean = new ArrayList<EntityBean>();
    private int mLength;


    public HomepagerAdapter(Context context, OnItemSelecter onItemSelecter) {
        this.mContext = context;
        this.mSelecter = onItemSelecter;
    }

    public void setImageUrls(EntityBean[] beanset) {
        mLength = beanset.length;
        clear();
        for (EntityBean bean : beanset) {
            EntityBean bean2 = bean;
            mBean.add(bean2);
        }
    }

    public void clear() {
        if (mBean != null) {
            mBean.clear();
        }
    }

    @Override
    public int getCount() {
        return 1000;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {

        return view == object;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {

        container.removeView((View) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        try {
            position = position % mLength;
            ImageView iv = new ImageView(mContext);
            iv.setScaleType(ScaleType.FIT_XY);
            final EntityBean bean = (EntityBean) mBean.get(position);
            String urlString = (String) bean.get("picpath");

            final int finalPosition = position;
            iv.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    if (mSelecter != null) {
                        mSelecter.onBananerClick(bean);
                        UmengStatisticsUtil.onEvent("banner" + (finalPosition + 1));
                    }
                }
            });
            if (!TextUtils.isEmpty(urlString)) {
                Glide.with(mContext).load(urlString).placeholder(R.drawable.by_banner).into(iv);
            }
            container.addView(iv);
            return iv;
        } catch (Exception e) {
        }
        return null;
    }

    public interface OnItemSelecter {
        void onBananerClick(EntityBean bean);
    }
}
